package com.mmall.example.immutable;

import com.google.common.collect.Maps;
import com.mmall.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.Map;

/**
 * Collections.unmodifiableMap 修饰Map (线程安全)
 * Created by megan on 2018/3/31.
 */
@Slf4j
@ThreadSafe
public class CollectionsExample {

    private static Map<Integer, Integer> map = Maps.newHashMap();

    static {
        map.put(1, 2);
        map.put(3, 4);
        map.put(5, 6);
        map.put(7, 8);
        map = Collections.unmodifiableMap(map);
    }

    public static void main(String[] args) {
        /** 不允许这样做-》Collections.unmodifiableMap 保证了线程的安全 **/
        map.put(1, 3);
        log.info("{}", map.get(1));
    }
}
